package cn.com.qing.ui;

import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;

import javafx.scene.control.Button;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebEvent;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import javafx.scene.control.TextField;

import javafx.scene.control.Label;

public class MainController {
	
	private String index ="https://www.baidu.com/";//http://www.javafxchina.net/

	private Stage primaryStage;

	@FXML
	private WebView webview;
	@FXML
	private Button btnok;
	@FXML
	private Label lbsearch;
	@FXML
	private TextField tfinput;

	protected void init() {
		WebEngine webEngine = webview.getEngine();
		webEngine.load(index);
		// url show
		webEngine.locationProperty().addListener((ObservableValue<? extends String> observable, String oldValue,
				String newValue) -> tfinput.setText(newValue));
		// title show
		webEngine.getLoadWorker().stateProperty().addListener(
				(ObservableValue<? extends Worker.State> observable, Worker.State oldValue, Worker.State newValue) -> {
					if (newValue == Worker.State.SUCCEEDED) {
						primaryStage.setTitle(webEngine.getTitle());
					}
				});
		// javascript alert window.alert("neirong")的html进行测试
		webEngine.setOnAlert((WebEvent<String> event) -> System.out.println("this is event" + event));

		// okclick
		btnok.setOnAction(
				(ActionEvent event) -> webEngine.load(tfinput.getText().startsWith("http://") ? tfinput.getText().trim()
						: "http://" + tfinput.getText().trim()));
	}

	public Stage getPrimaryStage() {
		return primaryStage;
	}

	public void setPrimaryStage(Stage primaryStage) {
		this.primaryStage = primaryStage;
	}

}
